#include <sys_config.h>

#ifdef USB_MP_SUPPORT
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>
#include <api/libmp/media_player_api.h>

#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"
#include "images.id"
#include "string.id"
#include "osd_config.h"

#include "control.h"
#include "menus_root.h"
#include "win_com.h"
#include "win_com_popup.h"
#include "win_com_list.h"
#include "win_media.h"
/*******************************************************************************
*	Objects definition
*******************************************************************************/
extern CONTAINER    g_win_slidesetup;

extern TEXT_FIELD slide_title;

extern CONTAINER slide_con_item1;
extern CONTAINER slide_con_item2;
//extern CONTAINER slide_con_item3;

//extern TEXT_FIELD   slide_size;
extern TEXT_FIELD   slide_time;
extern TEXT_FIELD   slide_repeat;
//extern TEXT_FIELD   slide_size_sel;
extern TEXT_FIELD   slide_time_sel;
extern TEXT_FIELD   slide_repeat_sel;

extern TEXT_FIELD   slide_line1;
extern TEXT_FIELD   slide_line2;
//extern TEXT_FIELD   slide_line3;

extern TEXT_FIELD  slide_save;
extern TEXT_FIELD  slide_exit;
/*******************************************************************************
*	Function decalare
*******************************************************************************/
#ifndef SD_UI
#define SLIDE_W_L	254//(210+180-136)
#define SLIDE_W_T	168//(70+98)
#define SLIDE_W_W	520
#define SLIDE_W_H	224
#define SLIDE_GAP	8

#define SLIDE_TITLE_L	(SLIDE_W_L + 50)
#define SLIDE_TITLE_T	(SLIDE_W_T + SLIDE_GAP * 2)
#define SLIDE_TITLE_W	(SLIDE_W_W- 100)
#define SLIDE_TITLE_H	36

#define BTN1_L (SLIDE_W_L + ((SLIDE_W_W/2) - BTN_W)/2)//(210+310)//208
#define BTN2_L (SLIDE_W_L + (SLIDE_W_W/2) + ((SLIDE_W_W/2) - BTN_W)/2)//(210+460)//312
#define BTN_T  (SLIDE_W_T + 176)//(70+314)//242
#define BTN_W  120
#define BTN_H  38

#define CON_L	(SLIDE_W_L + 30)
#define CON_T	(SLIDE_W_T + 70)
#define CON_W	(SLIDE_W_W - 60)
#define CON_H	40
#define CON_GAP 4

#define TXT_L_OF  	4
#define TXT_W  		260
#define TXT_H		40
#define TXT_T_OF	((CON_H - TXT_H)/2)

#define SEL_L_OF  (TXT_L_OF + TXT_W)
#define SEL_W  		(CON_W-SEL_L_OF)
#define SEL_H		40
#define SEL_T_OF	((CON_H - SEL_H)/2)

#define LINE_L_OF	0
#define LINE_T_OF  	CON_T
#define LINE_W		(CON_W)
#define LINE_H     	4
#else
#define SLIDE_W_L	100//(210+180-136)
#define SLIDE_W_T	100//(70+98)
#define SLIDE_W_W	400
#define SLIDE_W_H	224
#define SLIDE_GAP	8

#define SLIDE_TITLE_L	(SLIDE_W_L + 50)
#define SLIDE_TITLE_T	(SLIDE_W_T + SLIDE_GAP * 1)
#define SLIDE_TITLE_W	(SLIDE_W_W- 100)
#define SLIDE_TITLE_H	36

#define BTN1_L (SLIDE_W_L + ((SLIDE_W_W/2) - BTN_W)/2)//(210+310)//208
#define BTN2_L (SLIDE_W_L + (SLIDE_W_W/2) + ((SLIDE_W_W/2) - BTN_W)/2)//(210+460)//312
#define BTN_T  (SLIDE_W_T + 176)//(70+314)//242
#define BTN_W  120
#define BTN_H  30

#define CON_L	(SLIDE_W_L + 30)
#define CON_T	(SLIDE_W_T + 70)
#define CON_W	(SLIDE_W_W - 60)
#define CON_H	32
#define CON_GAP 4

#define TXT_L_OF  	4
#define TXT_W  		210
#define TXT_H		32
#define TXT_T_OF	((CON_H - TXT_H)/2)

#define SEL_L_OF  (TXT_L_OF + TXT_W)
#define SEL_W  		(CON_W-SEL_L_OF)
#define SEL_H		32
#define SEL_T_OF	((CON_H - SEL_H)/2)

#define LINE_L_OF	0
#define LINE_T_OF  	CON_T
#define LINE_W		(CON_W)
#define LINE_H     	4

#endif

#define SLIDE_SH_IDX    WSTL_MP_POP_WIN_01_HD
#define SLIDE_TITLE_IDX WSTL_TEXT_12_HD//WSTL_TEXT_04_HD
  
#define SLIDE_ITEM_SH   WSTL_BUTTON_01_FG_HD//sharon WSTL_MP_TEXT_03_HD//WSTL_TEXT_04_HD
#define SLIDE_ITEM_HL   WSTL_BUTTON_02_FG_HD//WSTL_BUTTON_05_HD
#define SLIDE_SET_SH    WSTL_BUTTON_01_FG_HD//WSTL_BUTTON_03_HD//sharon WSTL_MP_TEXT_03_HD//WSTL_TEXT_04_HD
#define SLIDE_SET_HL    WSTL_BUTTON_04_HD

#define BTN_SH_IDX	WSTL_BUTTON_POP_SH_HD
#define BTN_HL_IDX	WSTL_BUTTON_POP_HL_HD

#define CON_SH_IDX   WSTL_BUTTON_03_HD//sharon WSTL_MP_TEXT_03_HD//WSTL_TEXT_04_HD
#define CON_HL_IDX   WSTL_BUTTON_02_HD//sharon WSTL_BUTTON_03_HD//WSTL_BUTTON_05_HD
#define CON_SL_IDX   WSTL_BUTTON_08_HD//WSTL_BUTTON_01_HD
#define CON_GRY_IDX  WSTL_BUTTON_03_HD//WSTL_TEXT_20_HD

static VACTION setup_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT setup_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION slideset_btn_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT slideset_btn_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);
static VACTION slideset_item_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT slideset_item_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);
/*******************************************************************************
*	Objects Macro
*******************************************************************************/
#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
	DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
	0,0,0,0,0, l,t,w,h, SLIDE_SH_IDX,SLIDE_SH_IDX,SLIDE_SH_IDX,SLIDE_SH_IDX,   \
	setup_con_keymap,setup_con_callback,  \
	nxtObj, focusID,0)

#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    slideset_item_keymap,slideset_item_callback,  \
    conobj, ID,1)
    
#define LDEF_TITLE(root,varTxt,nxtObj,l,t,w,h,resID)		\
	DEF_TEXTFIELD(varTxt,&root,nxtObj,C_ATTR_ACTIVE,0, \
	0,0,0,0,0, l,t,w,h, \
	SLIDE_TITLE_IDX,SLIDE_TITLE_IDX,SLIDE_TITLE_IDX,SLIDE_TITLE_IDX,   \
	NULL,NULL,  \
	C_ALIGN_CENTER| C_ALIGN_VCENTER, 4,0,resID,NULL)
	
#define LDEF_TXT(root,varTxt,nxtObj,l,t,w,h,resID)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, SLIDE_ITEM_SH,SLIDE_ITEM_HL,SLIDE_ITEM_SH,SLIDE_ITEM_SH,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,NULL)

#define LDEF_TXT2(root,varTxt,nxtObj,l,t,w,h,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, SLIDE_SET_SH,SLIDE_SET_HL,SLIDE_SET_SH,SLIDE_SET_SH,   \
    NULL,NULL,  \
    C_ALIGN_CENTER| C_ALIGN_VCENTER, 0,0,0,str)
    
#define LDEF_TXT_BTN(root,varTxt,nxtObj,l,t,w,h,ID,IDl,IDr,IDu,IDd,resID)		\
    DEF_TEXTFIELD(varTxt,&root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, BTN_SH_IDX,BTN_HL_IDX,BTN_SH_IDX,BTN_SH_IDX,   \
    slideset_btn_keymap,slideset_btn_callback,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,resID,NULL)

#define LDEF_POP_LINE(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, WSTL_POP_LINE_HD,WSTL_POP_LINE_HD,WSTL_POP_LINE_HD,WSTL_LINE_HD,   \
    NULL,NULL,  \
    C_ALIGN_RIGHT | C_ALIGN_VCENTER, 10,0,resID,str)


#define LDEF_MENU_ITEM_SEL(root,varCon,nxtObj,varTxt,varSel,varLine,ID,IDu,IDd,\
						l,t,w,h,resID,str) \
    LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxt,ID)   \
    LDEF_TXT(&varCon,varTxt,&varSel,l + TXT_L_OF,t ,TXT_W,TXT_H,resID)    \
    LDEF_TXT2(&varCon,varSel,NULL/*&varLine*/,l + SEL_L_OF,t ,SEL_W,SEL_H,str)    \
	LDEF_POP_LINE(&varCon,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + CON_H,LINE_W,LINE_H,0,0)

/*******************************************************************************
*	Objects evaluate
*******************************************************************************/

LDEF_WIN(g_win_slidesetup,&slide_title,SLIDE_W_L,SLIDE_W_T,SLIDE_W_W,SLIDE_W_H,1)

LDEF_TITLE(g_win_slidesetup,slide_title,&slide_con_item1,SLIDE_TITLE_L,SLIDE_TITLE_T,SLIDE_TITLE_W,SLIDE_TITLE_H,RS_GAME_SETUP)

LDEF_MENU_ITEM_SEL(g_win_slidesetup,slide_con_item1,&slide_con_item2,slide_time,slide_time_sel,slide_line1, 1, 3, 2, \
		CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H, RS_MP_SLIDE_SHOW_TIME, display_strs[1]) 

LDEF_MENU_ITEM_SEL(g_win_slidesetup,slide_con_item2,&slide_save,slide_repeat,slide_repeat_sel,slide_line2, 2, 1, 3, \
		CON_L, CON_T + (CON_H + CON_GAP)*1, CON_W, CON_H, RS_MP_SLIDE_SHOW_REPEAT, NULL) 

LDEF_TXT_BTN(g_win_slidesetup,slide_save,&slide_exit,BTN1_L, BTN_T, BTN_W,BTN_H,3,4,4,2,1,RS_COMMON_SAVE)
LDEF_TXT_BTN(g_win_slidesetup,slide_exit,NULL,       BTN2_L, BTN_T, BTN_W,BTN_H,4,3,3,2,1,RS_COMMON_CANCEL)

TEXT_FIELD *txt_settings[] = 
{
	&slide_time_sel,
	&slide_repeat_sel,
};

static void win_open_setup_help(void);
static PRESULT win_setting_proc(VACTION unact);
static void win_setting_display(BOOL bDraw);

IMAGE_SLIDE_INFO image_slide_setting,image_slide_temp;

/*
UINT16 slide_size_strids[] =
{
	RS_MP_SLIDE_FULL_SIZE,
	RS_MP_SLIDE_REAL_SIZE
};
*/
UINT16 slide_repeat_strids[] = 
{
	RS_COMMON_OFF,
	RS_COMMON_ON,
};


#define TIME_ID 1
#define REPEAT_ID 2
#define BTN_SAVE_ID 3
#define BTN_EXIT_ID 4

#define VACT_SET_INCREASE (VACT_PASS + 1)
#define VACT_SET_DECREASE (VACT_PASS + 2)
#define VACT_SAVE_EXIT (VACT_PASS + 3)
#define VACT_ABANDON_EXIT (VACT_PASS + 4)
/*******************************************************************************
*	Function
*******************************************************************************/
static VACTION setup_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;

	switch(key)
	{
	case V_KEY_EXIT:
	case V_KEY_MENU:
		act = VACT_ABANDON_EXIT;
		break;
	case V_KEY_UP:
		act = VACT_CURSOR_UP;
		break;
	case V_KEY_DOWN:
		act = VACT_CURSOR_DOWN;
		break;
  	case V_KEY_LEFT:
        act = VACT_CURSOR_LEFT;
        break;
    case V_KEY_RIGHT:
        act = VACT_CURSOR_RIGHT;
		break;
	default:
		act = VACT_PASS;
	
	}

	return act;
}
static PRESULT setup_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	VACTION unact;
	PRESULT ret = PROC_PASS;

	switch(event)
	{
    	case EVN_PRE_OPEN:
		memcpy(&image_slide_temp,&image_slide_setting,sizeof(IMAGE_SLIDE_INFO));
		win_setting_display(FALSE);
		break;
	case EVN_POST_OPEN:
		break;
	case EVN_POST_CLOSE:
		break;
    case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16);
		ret = win_setting_proc(unact);
		break;
	default:
		break;
	}

	return ret;
}

static VACTION slideset_item_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;

	switch(key)
	{
	case V_KEY_LEFT:
		act = VACT_SET_DECREASE;
		break;
	case V_KEY_RIGHT:
		act = VACT_SET_INCREASE;
		break;
	default:
		act = VACT_PASS;
	
	}

	return act;

}

static PRESULT slideset_item_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	VACTION unact;
	PRESULT ret = PROC_PASS;

	switch(event)
	{
	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16);
		ret = win_setting_proc(unact);
		break;
	default:
		break;
	}

	return ret;
}

// save ,exit btn
static VACTION slideset_btn_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
    CONTAINER* win = &g_win_slidesetup;
    UINT8 bID;
    
	bID = OSD_GetContainerFocus(win);
	switch(key)
	{
	case V_KEY_ENTER:
        if(BTN_SAVE_ID == bID) 
		    act  = VACT_SAVE_EXIT;
        else
            act = VACT_ABANDON_EXIT;
		break;
	default:
		act = VACT_PASS;
	
	}

	return act;

}

static PRESULT slideset_btn_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{	
	VACTION unact;
	PRESULT ret = PROC_PASS;
    
	switch(event)
	{
	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16);
		ret = win_setting_proc(unact);
		break;
	default:
		break;
	}
	return ret;
}


void win_playlist_setup_open(void)
{
	UINT32 hkey;
	CONTAINER	*pop_win;
	PRESULT ret = PROC_LOOP;

    BOOL old_value = ap_enable_key_task_get_key(TRUE);
	pop_win = &g_win_slidesetup;
	OSD_SetContainerFocus(pop_win, 1);
	wincom_backup_region(&pop_win->head.frame);
	OSD_ObjOpen((POBJECT_HEAD)pop_win, 0);
	while(ret != PROC_LEAVE)
	{
		hkey = ap_get_key_msg();
		if(hkey == INVALID_HK || hkey == INVALID_MSG)
			continue;

		ret = OSD_ObjProc((POBJECT_HEAD)pop_win, (MSG_TYPE_KEY<<16),hkey, 0);
	}
	wincom_restore_region();
    ap_enable_key_task_get_key(old_value);
}


static PRESULT win_setting_proc(VACTION unact)
{
	UINT8 bID;
	CONTAINER*win;
	PRESULT ret = PROC_PASS;

	if((unact == VACT_SET_DECREASE) || (unact == VACT_SET_INCREASE))
	{
		ret = PROC_LOOP;
		win = &g_win_slidesetup;
		bID = OSD_GetContainerFocus(win);
		switch(bID)
		{
			case TIME_ID:
				if(unact == VACT_SET_INCREASE)
					image_slide_temp.uTime_gap = (image_slide_temp.uTime_gap + 1) % SLIDE_MAX_SEC;
				else
					image_slide_temp.uTime_gap = (image_slide_temp.uTime_gap + SLIDE_MAX_SEC - 1) % SLIDE_MAX_SEC;
				break;
			case REPEAT_ID:
				image_slide_temp.bRepeat = 1 - image_slide_temp.bRepeat;
				break;
			default:
				break;
		}

		win_setting_display(TRUE);
	}
	else if(unact == VACT_SAVE_EXIT)
	{
		memcpy(&image_slide_setting,&image_slide_temp,sizeof(IMAGE_SLIDE_INFO));
		ret = PROC_LEAVE;
	}
	else if(unact == VACT_ABANDON_EXIT)
	{
		ret = PROC_LEAVE;
	}


	return ret;
}

static void win_setting_display(BOOL bDraw)
{
	UINT8 i,bID;
	char str_buff[10];
	TEXT_FIELD *pTxt;
	CONTAINER *win;

	if(0 == image_slide_temp.uTime_gap)
		sprintf(str_buff, "Off");
	else
		sprintf(str_buff,"%d Sec",image_slide_temp.uTime_gap);

	win = &g_win_slidesetup;
	bID = OSD_GetContainerFocus(win);
	for(i = 0; i < 2; i++)
	{
		pTxt = txt_settings[i];
		if(i == 0)
			OSD_SetTextFieldContent(pTxt,STRING_ANSI,(UINT32)str_buff);
		else
			OSD_SetTextFieldContent(pTxt,STRING_ID,slide_repeat_strids[image_slide_temp.bRepeat]);

		if((bID == (i + 1)) && (bDraw))
		{
			OSD_TrackObject((POBJECT_HEAD)pTxt, C_UPDATE_ALL);
		}
	}
}


#endif

